﻿using DataAccessLayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utilities;

namespace BussinessLogicLayer
{
    public class TranDauBLL
    {
        static QuanLyGiaiDauDataContext QLGD = new QuanLyGiaiDauDataContext();
        public static List<DSTranDau> VongDau(int MaGD, string TenVD)
        {
            var trandau = from trd in QLGD.TranDaus
                          from dba in QLGD.DoiBongAs
                          from dbb in QLGD.DoiBongBs
                          from ttai in QLGD.TrongTais
                          where trd.MaGiaiDau == MaGD
                          && trd.TrangThai == "Bắt đầu"
                          && trd.VongDau == TenVD
                          && trd.DoiBongA == dba.MaDoiBong
                          && trd.DoiBongB == dbb.MaDoiBong
                          && trd.MaTrongTai == ttai.MaTrongTai
                          select new DSTranDau()
                          {
                              NTD = string.Format("{0:dd/MM/yyyy}", trd.NgayThiDau),
                              GD = string.Format("{0:HH:mm}", trd.GioDau),
                              TDBA = dba.TenDoiBong,
                              KQA = Convert.ToInt32(trd.KetQuaA),
                              KQB = Convert.ToInt32(trd.KetQuaB),
                              TDBB = dbb.TenDoiBong,
                              SD = trd.SanDau,
                              TTai = ttai.Ten
                          };
            return trandau.ToList();
        }
        public static List<QLTranDau> LayTatCaTD(int MaGD)
        {
            var trandau = from trd in QLGD.TranDaus
                          from dba in QLGD.DoiBongAs
                          from dbb in QLGD.DoiBongBs
                          from ttai in QLGD.TrongTais
                          where trd.MaGiaiDau == MaGD
                          && trd.TrangThai != "Đã xóa"
                          && trd.DoiBongA == dba.MaDoiBong
                          && trd.DoiBongB == dbb.MaDoiBong
                          && trd.MaTrongTai == ttai.MaTrongTai
                          select new QLTranDau()
                          {
                              MTD = trd.MaTranDau,
                              NTD = string.Format("{0:dd/MM/yyyy}", trd.NgayThiDau),
                              GD = string.Format("{0:HH:mm}", trd.GioDau),
                              TDBA = dba.TenDoiBong,
                              KQA = Convert.ToInt32(trd.KetQuaA),
                              KQB = Convert.ToInt32(trd.KetQuaB),
                              TDBB = dbb.TenDoiBong,
                              SD = trd.SanDau,
                              VD = trd.VongDau,
                              TTai = ttai.Ten,
                              TThai = trd.TrangThai
                          };
            return trandau.ToList();
        }
        public static bool ChiaTranDau(TranDau entity)
        {
            try
            {
                TranDau TD = new TranDau();
                TD.NgayThiDau = entity.NgayThiDau;
                TD.GioDau = entity.GioDau;
                TD.DoiBongA = entity.DoiBongA;
                TD.KetQuaA = entity.KetQuaA;
                TD.KetQuaB = entity.KetQuaB;
                TD.DoiBongB = entity.DoiBongB;
                TD.SanDau = entity.SanDau;
                TD.VongDau = entity.VongDau;
                TD.TrangThai = entity.TrangThai;
                QLGD.TranDaus.InsertOnSubmit(TD);
                QLGD.SubmitChanges();
                int kq = 1;
                return kq > 0;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }
        public static bool CapNhatTranDau(TranDau entity)
        {
            try
            {
                TranDau CapNhatTD = (from td in QLGD.TranDaus
                                     select td).Single(tdau => tdau.MaTranDau == entity.MaTranDau);
                CapNhatTD.NgayThiDau = entity.NgayThiDau;
                CapNhatTD.GioDau = entity.GioDau;
                CapNhatTD.DoiBongA = entity.DoiBongA;
                CapNhatTD.KetQuaA = entity.KetQuaA;
                CapNhatTD.KetQuaB = entity.KetQuaB;
                CapNhatTD.DoiBongB = entity.DoiBongB;
                CapNhatTD.SanDau = entity.SanDau;
                CapNhatTD.VongDau = entity.VongDau;
                CapNhatTD.TrangThai = entity.TrangThai;
                QLGD.SubmitChanges();
                int kq = 1;
                return kq > 0;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }
        public static bool XoaTranDau(int MaTD)
        {
            try
            {
                TranDau XoaTD = (from td in QLGD.TranDaus
                                 select td).Single(tdau => tdau.MaTranDau == MaTD);
                XoaTD.TrangThai = "Đã xóa";
                QLGD.SubmitChanges();
                int kq = 1;
                return kq > 0;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }
    }
}
